package cn.luke.basic.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.luke.basic.debounces.Debounce;
import cn.luke.basic.entity.DtoTemplate;
import cn.luke.basic.entity.DtoTemplateCampus;
import cn.luke.basic.log_api.LogApi;
import cn.luke.basic.rs.ResponseResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;


import cn.luke.basic.utils.MyAssert;


import cn.luke.basic.services.ServiceTemplateCampus;

import java.util.List;
import java.util.Map;


/**
 * 校区地址
 * pathName
 */

@RestController
@ResponseResult
@RequestMapping("/templateCampus")
public class ControllerTemplateCampus implements MyAssert {
    @Resource
    private ServiceTemplateCampus serviceTemplateCampus;


    /**
     * 校区地址-保存
     */
    @Debounce
    @LogApi("校区地址-保存")
    @PostMapping("/save")
    @SaCheckPermission("templateCampus.save")
    public void save(@RequestBody DtoTemplateCampus templateCampus) {
        strIsNull(templateCampus.getPhone(), "校区电话");
        intIsNull(templateCampus.getTemplateId(), "模板id");

        final long l = serviceTemplateCampus.countByTemplateId(templateCampus.getTemplateId());
        Assert.state(l == 0, "模板下有校区，不能添加");
        serviceTemplateCampus.save(templateCampus);
    }

    /**
     * 校区地址-状态修改
     */
    @Debounce
    @GetMapping("/updateStatus")
    public void updateStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status, @RequestParam(
            "templateId") Integer templateId) {

        serviceTemplateCampus.updateStatus(id, status,templateId);
    }

    /**
     * 校区地址-编辑
     */
    @Debounce
    @LogApi("校区地址-编辑")
    @PostMapping("/update")
    public void update(@RequestBody DtoTemplateCampus template) {
        intIsNull(template.getId(), "模板ID");
        idIsNull(template.getTemplateId(), "id");
        serviceTemplateCampus.updateById(template);
    }


    /**
     * 删除
     */
    @Debounce
    @LogApi("删除")
    @GetMapping("/delete/{id}/{templateId}")
    public void deleteById(@PathVariable("id") Integer id, @PathVariable("templateId") Integer templateId) {
        serviceTemplateCampus.deleteById(id, templateId);
    }


    /**
     * 校区地址-查询
     */
    @Debounce
    @PostMapping("/list")
    public List<DtoTemplateCampus> list(@RequestBody Map<String, Object> params) {
        intIsNull(params.get("templateId"), "模板ID");
        return serviceTemplateCampus.findListByParams(params);
    }

}
